package cn.stylefeng.guns.sys.modular.util;


import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

/**
 * @Author: liujianqun
 * @Description:
 * @Date: 2017/8/7
 * @Moidfy by:
 */

public class CryptoUtil {


    private static CertificateFactory factory = initFactory();

    private static CertificateFactory initFactory() {
        try {
            return CertificateFactory.getInstance("X.509");
        } catch (CertificateException var1) {
            throw new RuntimeException(var1);
        }
    }

    /**
     * @return java.lang.String
     * @desc SHA256 加密公共类
     * @author liujianqun
     * @method getSHA256
     */

    public static X509Certificate base64StrToCert(String base64Cert) throws GeneralSecurityException {
        try {
            ByteArrayInputStream ex = new ByteArrayInputStream((new BASE64Decoder()).decodeBuffer(base64Cert));
            X509Certificate cert = (X509Certificate) factory.generateCertificate(ex);
            if (cert == null) {
                throw new GeneralSecurityException("将cer从base64转换为对象失败");
            } else {
                return cert;
            }
        } catch (IOException var3) {
            throw new GeneralSecurityException("将cer从base64转换为对象失败", var3);
        }
    }



}
